﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAO;
using System.Data;
using System.Text.RegularExpressions;

namespace BUS
{
    public class BanAnBUS
    {
        public string ConvertToUnSign(string text)
        {

            for (int i = 33; i < 48; i++)
            {

                text = text.Replace(((char)i).ToString(), "");

            }



            for (int i = 58; i < 65; i++)
            {

                text = text.Replace(((char)i).ToString(), "");

            }



            for (int i = 91; i < 97; i++)
            {

                text = text.Replace(((char)i).ToString(), "");

            }

            for (int i = 123; i < 127; i++)
            {

                text = text.Replace(((char)i).ToString(), "");

            }

            text = text.Replace(" ", " ");

            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");

            string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);

            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');

        }
        public DataTable LoadDS()
        {
            BanAnDAO baDAO = new BanAnDAO();
            return baDAO.LoadDS();
        }

        public Boolean CapNhat(BanAnDTO baDTO)
        {
            BanAnDAO baDAO = new BanAnDAO();
            return baDAO.CapNhat(baDTO);
        }

        public Boolean KiemTraTrungTenBan(string tenban)
        {
            BanAnDAO baDAO = new BanAnDAO();
            DataTable dt = new DataTable();
            dt = baDAO.DanhSachTenBan();

            foreach (DataRow x in dt.Rows)
            {
                string name1=ConvertToUnSign(x[0].ToString().ToLower().Replace(" ",""));
                string name2=ConvertToUnSign(tenban.ToLower().Replace(" ",""));

                if(string.Compare(name1,name2)==0)
                {
                    return true;

                }
            }
            return false;
        }

        public Boolean ThemBan(BanAnDTO baDTO)
        {
            BanAnDAO baDAO = new BanAnDAO();
            return baDAO.ThemBan(baDTO);
        }

        public Boolean XoaBan(int maBan)
        {
            BanAnDAO baDAO = new BanAnDAO();
            return baDAO.XoaBan(maBan);
        }

        public Boolean CapNhatBanTrung(string tenban, int succhua)
        {
            BanAnDAO baDAO = new BanAnDAO();
            return baDAO.CapNhatBanTrung(tenban, succhua);
        }
    }
}
